/**
 * Work with channels.
 *
 * The channel class provides methods for a client to establish a virtual
 *   connection - a channel - to a server and for both peers to operate the
 *   virtual connection thereafter.
 *
 * @name Channel
 * @exports Channel as api.Channel
 * @class
 */
function Channel(/**Channel*/handler){
    var that = this;
    handler.addListener('Channel', function(frame){
        switch(frame.method.methodId){

        case 11:
/**
 * Signal that the channel is ready.
 *
 * This method signals to the client that the channel is ready for use.
 *
 * @name Channel#openOk
 * @event
 * 
 */
            that.emit('openOk', frame);
            break;

        case 20:
/**
 * Enable/disable flow from peer.
 *
 * This method asks the peer to pause or restart the flow of content
 *     data. This is a simple flow-control mechanism that a peer can use
 *     to avoid oveflowing its queues or otherwise finding itself receiving
 *     more messages than it can process.  Note that this method is not
 *     intended for window control.  The peer that receives a request to
 *     stop sending content should finish sending the current content, if
 *     any, and then wait until it receives a Flow restart method.
 *
 * @name Channel#flow
 * @event
 * @param {Boolean} active If 1, the peer starts sending content frames.  If 0, the peer
 *       stops sending content frames.
 */
            that.emit('flow', frame);
            break;

        case 21:
/**
 * Confirm a flow method.
 *
 * Confirms to the peer that a flow command was received and processed.
 *
 * @name Channel#flowOk
 * @event
 * @param {Boolean} active Confirms the setting of the processed flow method: 1 means the
 *       peer will start sending or continue to send content frames; 0
 *       means it will not.
 */
            that.emit('flowOk', frame);
            break;

        case 30:
/**
 * Send a non-fatal warning message.
 *
 * This method allows the server to send a non-fatal warning to the
 *     client.  This is used for methods that are normally asynchronous
 *     and thus do not have confirmations, and for which the server may
 *     detect errors that need to be reported.  Fatal errors are handled
 *     as channel or connection exceptions; non-fatal errors are sent
 *     through this method.
 *
 * @name Channel#alert
 * @event
 * @param {Number} replyCode The reply code. The AMQ reply codes are defined in AMQ RFC 011.
 * @param {String} replyText The localised reply text.  This text can be logged as an aid to
 *   resolving issues.
 * @param {Object} details A set of fields that provide more information about the
 *       problem.  The meaning of these fields are defined on a
 *       per-reply-code basis (TO BE DEFINED).
 */
            that.emit('alert', frame);
            break;

        case 40:
/**
 * Request a channel close.
 *
 * This method indicates that the sender wants to close the channel.
 *     This may be due to internal conditions (e.g. a forced shut-down) or
 *     due to an error handling a specific method, i.e. an exception.  When
 *     a close is due to an exception, the sender provides the class and
 *     method id of the method which caused the exception.
 *
 * @name Channel#close
 * @event
 * @param {Number} replyCode The reply code. The AMQ reply codes are defined in AMQ RFC 011.
 * @param {String} replyText The localised reply text.  This text can be logged as an aid to
 *   resolving issues.
 * @param {Number} classId 
 * @param {Number} methodId 
 */
            that.emit('close', frame);
            break;

        case 41:
/**
 * Confirm a channel close.
 *
 * This method confirms a Channel.Close method and tells the recipient
 *     that it is safe to release resources for the channel and close the
 *     socket.
 *
 * @name Channel#closeOk
 * @event
 * 
 */
            that.emit('closeOk', frame);
            break;

        }
    });
}
require('sys').inherits(Channel, require('events').EventEmitter);


/**
 * Open a channel for use.
 *
 * This method opens a virtual connection (a channel).
 *
 * @name Channel#open
 * @function
 * @param {String} outOfBand Configures out-of-band transfers on this channel.  The syntax and
 *       meaning of this field will be formally defined at a later date.
 */

/**
 * Enable/disable flow from peer.
 *
 * This method asks the peer to pause or restart the flow of content
 *     data. This is a simple flow-control mechanism that a peer can use
 *     to avoid oveflowing its queues or otherwise finding itself receiving
 *     more messages than it can process.  Note that this method is not
 *     intended for window control.  The peer that receives a request to
 *     stop sending content should finish sending the current content, if
 *     any, and then wait until it receives a Flow restart method.
 *
 * @name Channel#flow
 * @function
 * @param {Boolean} active If 1, the peer starts sending content frames.  If 0, the peer
 *       stops sending content frames.
 */

/**
 * Confirm a flow method.
 *
 * Confirms to the peer that a flow command was received and processed.
 *
 * @name Channel#flowOk
 * @function
 * @param {Boolean} active Confirms the setting of the processed flow method: 1 means the
 *       peer will start sending or continue to send content frames; 0
 *       means it will not.
 */

/**
 * Request a channel close.
 *
 * This method indicates that the sender wants to close the channel.
 *     This may be due to internal conditions (e.g. a forced shut-down) or
 *     due to an error handling a specific method, i.e. an exception.  When
 *     a close is due to an exception, the sender provides the class and
 *     method id of the method which caused the exception.
 *
 * @name Channel#close
 * @function
 * @param {Number} replyCode The reply code. The AMQ reply codes are defined in AMQ RFC 011.
 * @param {String} replyText The localised reply text.  This text can be logged as an aid to
 *   resolving issues.
 * @param {Number} classId 
 * @param {Number} methodId 
 */

/**
 * Confirm a channel close.
 *
 * This method confirms a Channel.Close method and tells the recipient
 *     that it is safe to release resources for the channel and close the
 *     socket.
 *
 * @name Channel#closeOk
 * @function
 * 
 */

exports.Entity = Channel;
